// Script Animator
// (c) jimon game studio

#ifndef JEH_JESCRIPTANIMATOR
#define JEH_JESCRIPTANIMATOR

#include "ISceneManager.h"
#include "IAnimator.h"

#include "IFileSystem.h"
#include "IScriptManager.h"

#include "ITimer.h"
#include "ILogManager.h"

namespace je
{
	namespace scene
	{
		//! Script Animator
		class jeScriptAnimator:public je::scene::IAnimator
		{
		protected:
			core::IScriptObject * Object;
			jeStringc ScriptClassName;
		public:
			//! Constructor
			jeScriptAnimator()
				:IAnimator("ScriptAnimator"),Object(NULL)
			{
			}

			//! Destructor
			~jeScriptAnimator()
			{
				Object->Drop();
			}

			//! Set Script Class Name
			void SetScriptClassName(const jeStringc & SetScriptClassName)
			{
				ScriptClassName = SetScriptClassName;

				if(Object)
					Object->Drop();

				Object = core::GetScriptManager()->CreateScriptObject(ScriptClassName);
			}

			//! Set Node
			void SetNode(ISceneNode * Node)
			{
				ParentNode = Node;
				if(Object)
				{
					core::GetScriptManager()->CallMethod("void SetNode(ISceneNode&)",Object);
					core::GetScriptManager()->AddParam(ParentNode);
					core::GetScriptManager()->ExecuteCall();
				}
			}

			//! On Update
			void OnUpdate()
			{
				core::GetScriptManager()->CallMethod("void OnUpdate()",Object);
				core::GetScriptManager()->ExecuteCall();
				IAnimator::OnUpdate();
			}

			//! Serialize
			void Serialize(core::ISerializeStream * SerializeStream)
			{
				JE_SS_BEGIN(ScriptAnimator)

				JE_SS(ScriptClassName)

				if(JE_SS_LOAD)
					SetScriptClassName(ScriptClassName);

				core::GetScriptManager()->CallMethod("void Serialize(ISerializeStream&)",Object);
				core::GetScriptManager()->AddParam(SerializeStream);
				core::GetScriptManager()->ExecuteCall();

				if(JE_SS_LOAD)
					SetNode(ParentNode);

				JE_SS_END(ScriptAnimator)

				IAnimator::Serialize(SerializeStream);
			}
		};
	}
}

#endif
